package no.hio.wsclient.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;

import no.hio.wsclient.vo.ProductStock;
import no.hio.wsproviderproduct.Product;
import no.hio.wsproviderstock.StockItem;

public class ProductStockDAOImpl implements ProductStockDAO {

	ProductDAO prodDAO;
	StockItemDAO stockIDAO;
	
	/* (non-Javadoc)
	 * @see no.hio.wsclient.dao.ProductStockDAO#findAllProducts()
	 */
	@Override
	public List<ProductStock> findAllProducts() {
		List<ProductStock> retList = new ArrayList<ProductStock>();
		
		List<Product> productList = prodDAO.findAllProducts();
		
		for(Product p : productList)
		{
			StockItem s = stockIDAO.findByProductID(p.getProductID());
			
			retList.add(combine(p,s));
			
		}
		
		return retList;
	}
	
	private ProductStock combine(Product p, StockItem s)
	{
		if(p.getProductID() != s.getProductID()) 
			throw new UnsupportedOperationException("Both product and stockitem must have same product id");
		
		ProductStock ps = new ProductStock();
		
		ps.setDescription(p.getDescription());
		ps.setNumInStock(s.getNumInStock());
		ps.setPrice(p.getPrice());
		ps.setProductID(p.getProductID());
		ps.setStockLocation(s.getStockLocation());
		ps.setTitle(p.getTitle());
		
		return ps;
	}

	/* (non-Javadoc)
	 * @see no.hio.wsclient.dao.ProductStockDAO#findByProductID(int)
	 */
	@Override
	public ProductStock findByProductID(int productID) {
		StockItem s = stockIDAO.findByProductID(productID);
		Product p = prodDAO.findByProductID(productID);
		if(p == null || s == null) return null;
		
		ProductStock ps = combine(p, s);
		
		return ps;
	}

	/* (non-Javadoc)
	 * @see no.hio.wsclient.dao.ProductStockDAO#insertProduct(no.hio.wsclient.vo.ProductStock)
	 */
	@Override
	public int insertProduct(ProductStock ps)
			throws UnsupportedOperationException {

		ps.setProductID(prodDAO.insertProduct(getProduct(ps)));
		
		if(ps.getProductID() == -1) return -1;
		
		stockIDAO.insertStockItem(getStockItem(ps));
		
		
		
		
		return ps.getProductID();
	}
	
	private Product getProduct(ProductStock ps)
	{
	
		return ps.getProduct();
	}
	
	private StockItem getStockItem(ProductStock ps)
	{
		return ps.getStockItem();
	}

	/* (non-Javadoc)
	 * @see no.hio.wsclient.dao.ProductStockDAO#removeProduct(no.hio.wsclient.vo.ProductStock)
	 */
	@Override
	public void removeProduct(ProductStock ps)
			throws NoSuchElementException {
		//must check that stockItem exists, if not, do not remove product
		if(stockIDAO.findByProductID(ps.getProductID())==null) throw new NoSuchElementException();
		//if product does not exist, stock item is not removed.
		prodDAO.removeProduct(ps.getProduct());
		stockIDAO.removeStockItem(ps.getStockItem());
		
	}

	/* (non-Javadoc)
	 * @see no.hio.wsclient.dao.ProductStockDAO#setProductDAO(no.hio.wsclient.dao.ProductDAO)
	 */
	@Override
	public void setProductDAO(ProductDAO productDAO) {
		prodDAO = productDAO;
	
	}

	/* (non-Javadoc)
	 * @see no.hio.wsclient.dao.ProductStockDAO#setStockItemDAO(no.hio.wsclient.dao.StockItemDAO)
	 */
	@Override
	public void setStockItemDAO(StockItemDAO stockItemDAO) {
		stockIDAO = stockItemDAO;
	}

	/* (non-Javadoc)
	 * @see no.hio.wsclient.dao.ProductStockDAO#updateProduct(no.hio.wsclient.vo.ProductStock)
	 */
	@Override
	public void updateProduct(ProductStock ps)
			throws NoSuchElementException {
		if(stockIDAO.findByProductID(ps.getProductID()) == null) 
			throw new NoSuchElementException();
		
		prodDAO.updateProduct(ps.getProduct());
		stockIDAO.updateStockItem(ps.getStockItem());
		
	}



	
}
